package com.darrenchan.leetcode;

import java.util.*;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2020-11-01 11:27
 */
public class Test2 {
    public boolean canFormArray(int[] arr, int[][] pieces) {

        Collections.sort(new ArrayList<Integer>(), (o1, o2) -> 0);
        int size = 0;
        Map<Integer, int[]> map = new HashMap<>();
        for(int i = 0; i < pieces.length; i++) {
            size += pieces[i].length;
            map.put(pieces[i][0], pieces[i]);
        }

        if(size != arr.length) {
            return false;
        }


        int step;
        for(int i = 0; i < arr.length; i += step) {
            int ch = arr[i];
            step = 1;

            if(map.containsKey(ch)) {
                int[] piece = map.get(ch);
                while(i + step < arr.length && step < piece.length) {
                    if(arr[i + step] != piece[step]) {
                        return false;
                    }
                    step++;
                }
            } else {
                return false;
            }
        }

        return true;
    }



//    public int[] getRandom(int[] arr) {
//        int num = 0;
//        int[] res = new int[1000];
//        int len = 1000000;
//        int last = len - 1;
//        while(num < 1000){
//            int index = Random.random(0, len);
//            res[num] = arr[index];
//
//
//            int temp = arr[index];
//            arr[index] = arr[last];
//            arr[last] = temp;
//
//            last--;
//            len--;
//            num++;
//        }
//
//
//        return res;
//    }

    public void getSecondMax(int[] arr) {
        int max = arr[0];
        int second = Integer.MIN_VALUE;

        for (int i = 1; i < arr.length; i++) {
            if(max < arr[i]) {
                second = max;
                max = arr[i];
            } else {
                if(second < arr[i]) {
                    second = arr[i];
                }
            }
        }

        System.out.println(max + "," + second);
    }



    public static void main(String[] args) {
//        System.out.println(new Test2().canFormArray(new int[]{9, 4, 64, 78}, new int[][]{{78}, {4, 64}, {9}}));
        new Test2().getSecondMax(new int[]{1,2,3,4,5,6,7,8,9});
    }
}
